package com.imooc.news_manager_project.controller;

import com.imooc.news_manager_project.common.Result;
import com.imooc.news_manager_project.common.validator.SaveValidator;
import com.imooc.news_manager_project.dto.UserDTO;
import com.imooc.news_manager_project.query.UserQuery;
import com.imooc.news_manager_project.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


@Api(tags = "用户管理")
@RestController
@RequestMapping("/user")
//用户管理
public class UserController {
    @Autowired
    private IUserService userService;

    @ApiImplicitParam(paramType = "header", name = "token", value = "登录获取的token值", required = true)
    @ApiOperation(value = "创建新用户")
    @PostMapping("/save")
    public Result save(@RequestBody @Validated(SaveValidator.class) UserDTO userDTO) {
        return userService.save(userDTO);
    }

    @ApiImplicitParam(paramType = "header", name = "token", value = "登录获取的token值", required = true)
    @ApiOperation(value = "条件查询用户数据")
    @PostMapping("/list")
    public Result list(@RequestBody UserQuery userQuery) {
        return userService.listParams(userQuery);
    }

    @ApiImplicitParam(paramType = "header", name = "token", value = "登录获取的token值", required = true)
    @ApiOperation(value = "获取全部用户")
    @GetMapping("/lists")
    public Result lists() {
        return userService.lists();
    }

    @ApiImplicitParam(paramType = "header", name = "token", value = "登录获取的token值", required = true)
    @ApiOperation(value = "更新用户")
    @PutMapping("/update")
    public Result update(@RequestBody UserDTO userDTO) {
        return userService.update(userDTO);
    }

    @ApiImplicitParam(paramType = "header", name = "token", value = "登录获取的token值", required = true)
    @ApiOperation(value = "删除用户")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") Integer id) {
        return userService.remove(id);
    }
}
